<template>
  <a-spin :spinning='confirmLoading'>
    <j-form-container :disabled='formDisabled'>
      <a-form slot='detail' :form='form'>
        <a-row>
          <a-col :span='24'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-button :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"' icon='plus' style='margin-left:50px'
                        type='primary'
                        @click='projectSelectClick'>复制
              </a-button>
              <a-input v-show='false' v-decorator="['addType']"></a-input>
            </a-form-item>
          </a-col>
          <!--          <a-col :span="12">-->
          <!--            <a-form-item label="项目简称" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--              <a-input-->
          <!--                v-decorator="['projectName', validatorRules.projectShortName]"-->
          <!--                placeholder="请选择项目简称"-->
          <!--                :disabled ='model.bpmStatus==="2"&&model.isDuty==="Y"'-->
          <!--                @click="projectShortNameClick"-->
          <!--              ></a-input>-->
          <!--              <a-input v-decorator="['projectId']" v-show='false'></a-input>-->
          <!--              <a-input v-decorator="['projectCode']" v-show='false'></a-input>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <!--          <a-col :span="12">-->
          <!--            <a-form-item label="项目专题" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--              <a-input-->
          <!--                v-decorator="['subjectName']"-->
          <!--                placeholder="请选择专题名称"-->
          <!--                :disabled ='model.bpmStatus==="2"&&model.isDuty==="Y"'-->
          <!--                @click="subjectNameClick()"-->
          <!--              ></a-input>-->
          <!--              <a-input v-decorator="['subjectId']" v-show='false'></a-input>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='项目名称'>
              <j-popup
                v-decorator="['projectName',validatorRules.projectShortName]"
                :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                :trigger-change='true'
                code='pm_project_select'
                dest-fields='projectId,projectName'
                org-fields='id,short_name'
                @callback='popupCallback'
              />
              <input v-show='false' v-decorator="['projectId']"></input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='项目专题'>
              <j-popup
                v-decorator="['subjectName']"
                :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                :param='{projectId: model.projectId}'
                :trigger-change='true'
                code='pm_subject_select'
                dest-fields='subjectId,subjectName'
                org-fields='id,subject_name'
                @callback='popupCallback'
              />
              <input v-show='false' v-decorator="['subjectId']"></input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='项目经理'>
              <a-input v-decorator="['charger',validatorRules.charger]" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='提出单位'>
              <a-input v-decorator="['proposeDep', validatorRules.proposeDep]"
                       :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                       placeholder='请输入提出单位'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='对方提出人'>
              <a-input v-decorator="['proposePersion', validatorRules.proposePersion]"
                       :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                       placeholder='请输入提出人'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='联系方式'>
              <a-input v-decorator="['contact', validatorRules.contact]"
                       :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                       placeholder='请输入联系方式'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='所属模块'>
              <a-input v-decorator="['beginDes', validatorRules.beginDes]"
                       :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                       placeholder='请输入所属模块'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='问题类型'>
              <j-dict-select-tag v-decorator="['questionType',validatorRules.questionType]"
                                 :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                                 :trigger-change='true' dictCode='question_type' placeholder='请选择问题类型!'
                                 type='list' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='期望时间'>
              <j-date v-decorator="['expectedTime', validatorRules.expectedTime]"
                      :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                      :trigger-change='true' placeholder='请选择期望时间'
                      style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='负责人'>
              <j-select-user-by-dep v-decorator="['dutyPersion',validatorRules.confim]"
                                    :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                                    :multi='false'></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col v-if='(model.bpmStatus==="2"&&model.isDuty==="Y")||model.bpmStatus==="3"' :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='预估人天'>
              <a-input v-decorator="['estimateWorkday', validatorRules.estimateWorkday]"
                       placeholder='请输入预估人天'></a-input>
            </a-form-item>
          </a-col>

          <a-col v-if='(model.bpmStatus==="2"&&model.isDuty==="Y")||model.bpmStatus==="3"' :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='处理方式'>
              <j-dict-select-tag v-decorator="['dealType',validatorRules.dealType]" :trigger-change='true'
                                 dictCode='deal_function'
                                 placeholder='请选择处理方式（字典：现场、公司）' type='list' />
            </a-form-item>
          </a-col>
          <a-col v-if='(model.bpmStatus==="2"&&model.isDuty==="Y")||model.bpmStatus==="3"' :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='预计完成时间'>
              <j-date v-decorator="['endTime', validatorRules.endTime]" :trigger-change='true' placeholder='请选择完成结束时间'
                      style='width: 100%' />
            </a-form-item>
          </a-col>


          <a-col :span='24'>
            <a-form-item :labelCol='labelCol2' :wrapperCol='wrapperCol2' label='问题描述'>
              <a-textarea v-decorator="['questionDes',validatorRules.questionDes]" :autoSize='{minRows:4}'
                          :disabled='model.bpmStatus==="2"&&model.isDuty==="Y"'
                          placeholder='请输入问题描述...
前置条件...
后置条件...
字段信息...
非功能要求...
设计约束...
其他补充...'
              />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='附件'>
              <j-upload v-decorator="['accessories']" :trigger-change='true'></j-upload>
            </a-form-item>
          </a-col>
          <a-col v-if='showFlowSubmitButton' :span='24' style='text-align: center'>
            <a-button @click='submitForm'>提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <hf-project-select-modal ref='hfProjectSelectModal' :multiple='false' @hf-pm-project-select='hfPmProjectSelect' />
    <hf-subject-select-modal ref='hfSubjectSelectModal' :multiple='false' @hf-subject-select='hfSubjectSelect' />
    <hf-pm-project-question-choose-modal ref='hfPmProjectQuestionChooseModal' :multiple='false'
                                         @hf-pm-project-question-choose='projectSelect' />
  </a-spin>
</template>

<script>

import { getAction, httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import HfProjectSelectModal from '@views/hifar/pm/modules/HfProjectSelectModal'
import HfSubjectSelectModal from '@views/hifar/pm/modules/HfSubjectSelectModal'
import HfPmProjectQuestionChooseModal from '@views/hifar/pm/modules/HfPmProjectQuestionChooseModal'
import JImageUpload from '@comp/jeecg/JImageUpload'

export default {
  name: 'HfPmProjectQuestionsForm',
  components: {
    HfProjectSelectModal,
    HfSubjectSelectModal,
    HfPmProjectQuestionChooseModal,
    JImageUpload
  },
  watch: {
    //流程表单的提交的监听事件，用来监听修改或者操作流程的表单
    // 同时需要修改 submitForm方法

    '$store.state.bpm.bpmSubmitStates': {
      immediate: true,
      handler(val) {
        if (val && val.edit && val.edit === '1') {
          this.submitForm().then(res => {
            if (res) {
              this.$store.commit('BPM_SUBMIT_STATES', {edit: '2'})
            }
          })
        }
      },
    }
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    //处理显示
    disposes: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      fileList: [],
      form: this.$form.createForm(this),
      model: {},
      addType: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      confirmLoading: false,
      validatorRules: {
        projectShortName: {
          rules: [
            { required: true, message: '请选择项目简称!' }
          ]
        },
        subjectName: {
          rules: [
            { required: true, message: '请选择专题名称!' }
          ]
        },
        beginDes: {
          rules: [
            { required: true, message: '请填写所属模块!' }
          ]
          // initialValue:"0"
        },
        questionCode: {
          rules: [
            { required: true, message: '系统自动生成!' }
          ]
        },
        charger: {
          rules: [
            { required: true, message: '系统自动带出!' }
          ]
        },
        proposeDep: {
          rules: [
            { required: true, message: '请输入提出单位!' }
          ]
        },
        proposePersion: {
          rules: [
            { required: true, message: '请输入提出人!' }
          ]
        },
        contact: {
          rules: [
            { required: true, message: '请输入联系方式!' }
          ]
        },
        expectedTime: {
          rules: [
            { required: true, message: '请输入期望时间!' }
          ]
        },
        endTime: {
          rules: [
            { required: true, message: '请输入预计完成时间!' }
          ]
        },
        estimateWorkday: {
          rules: [
            { required: true, message: '请输入预估人天!' },
            { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
          ]
        },
        questionDes: {
          rules: [
            { required: true, message: '请输入问题描述!' }
          ]
        },
        confim: {
          rules: [
            { required: true, message: '请选择问题负责人!' }
          ]
        },
        dealType: {
          rules: [
            { required: true, message: '请选择处理方式!' }
          ]
        },
        questionType: {
          rules: [
            { required: true, message: '请选择处理方式!' }
          ]
        }
      },
      url: {
        add: '/pm/hfPmProjectQuestions/add',
        edit: '/pm/hfPmProjectQuestions/edit',
        queryById: '/pm/hfPmProjectQuestions/queryById',
        upload: '/pm/hfPmProjectQuestions/uploadFile'
      },
      fields: [
        'createBy', 'createTime', 'questionCode', 'projectId', 'projectCode', 'projectName', 'subjectId', 'subjectName',
        'subjectCode', 'proposeDep', 'proposePersion', 'questionDes', 'contact', 'expectedTime', 'dutyPersion', 'confirmorId',
        'confirmorName', 'confirmorDes', 'estimateWorkday', 'dealType', 'questionState', 'charger', 'addType', 'businessDes', 'beginDes',
        'afterDes', 'colnumMes', 'demandMes', 'designMes', 'ortherMes', 'picMes', 'bpmStatus', 'endTime', 'isDuty', 'questionType', 'accessories'
      ]
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    add() {
      let defaultObj = {}
      let {
        selectedSubName,
        selectedName,
        selectedKeys,
        selectedSubKeys,
        selectedCode,
        selectedSubCode,
        selectedTreeType,
        selectedCharger
      } = this.defaultModel
      if (selectedTreeType === 'project') {
        defaultObj = {
          projectName: selectedName,
          projectId: selectedKeys,
          projectCode: selectedCode,
          charger: selectedCharger
        }
      } else if (selectedTreeType === 'subject') {
        defaultObj = {
          projectName: selectedName,
          projectId: selectedKeys,
          projectCode: selectedCode,
          subjectName: selectedSubName,
          subjectCode: selectedSubCode,
          subjectId: selectedSubKeys,
          charger: selectedCharger
        }
      }
      this.edit(defaultObj)
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      return new Promise(((resolve, reject) => {
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                resolve(true)
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          } else {
            this.$store.commit('BPM_SUBMIT_STATES', {})
          }
        })
      }))
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    },
    projectShortNameClick() {
      this.$nextTick(() => {
        this.$refs.hfProjectSelectModal.title = '选择项目'
        this.$refs.hfProjectSelectModal.visible = true
      })
    },
    hfPmProjectSelect(data) {
      this.form.setFieldsValue({
        projectId: data.id,
        projectName: data.shortName,
        projectCode: data.code,
        charger: data.charger
      })
    },
    projectSelectClick() {
      this.form.setFieldsValue({
        addType: '1'
      })
      this.$nextTick(() => {
        this.$refs.hfPmProjectQuestionChooseModal.title = '选择项目'
        this.$refs.hfPmProjectQuestionChooseModal.visible = true
      })
    },
    projectSelect(data) {
      this.form.setFieldsValue({
        projectId: data.projectId,
        projectName: data.projectName,
        projectCode: data.projectCode,
        proposeDep: data.proposeDep,
        proposePersion: data.proposePersion,
        contact: data.contact,
        estimateWorkday: data.estimateWorkday,
        dutyPersion: data.dutyPersion,
        dealType: data.dealType,
        createBy: data.createBy,
        createTime: data.createTime,
        subjectName: data.subjectName,
        subjectId: data.subjectId,
        expectedTime: data.expectedTime,
        charger: data.charger
      })
    },
    subjectNameClick() {
      this.$nextTick(() => {
        this.$refs.hfSubjectSelectModal.title = '选择专题'
        this.$refs.hfSubjectSelectModal.visible = true
      })
    },
    hfSubjectSelect(data) {
      this.form.setFieldsValue({
        subjectId: data.id,
        subjectName: data.subjectName,
        subjectCode: data.subjectCode
      })
    }
  }
}
</script>